home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / tclMotif-1.4 / tests / list.test < prev    next >
Encoding:
Text File  |  1995-06-29  |  8.3 KB  |  404 lines

  1.  
  2. if {[string compare test [info procs test]] == 1} then \
  3.   {source defs}
  4.  
  5. # set VERBOSE 1
  6. # set INTERACTIVE 1
  7.  
  8. ############
  9. # work procs
  10. ############
  11.  
  12. proc listItems {} {
  13.   .listform.list getValues \
  14.     -items i -itemCount n
  15.   return "$n $i"
  16. }
  17.  
  18. proc listSelectedItems {} {
  19.   .listform.list getValues \
  20.     -selectedItems i -selectedItemCount n
  21.   return "$n $i"
  22. }
  23.  
  24. proc indexToY {w n} {
  25.   # calculate the Y co-ord for an index in the list
  26.   $w getValues \
  27.     -height h -itemCount count \
  28.     -visibleItemCount vis
  29.  
  30.   set y [expr {(2*$n - 1) * $h / (2*$count)}]
  31.   return $y
  32. }
  33.  
  34. proc pause {} {
  35.   # pause, so that repeated button clicks are not
  36.   # mistaken for double clicks!
  37.  
  38. # this stuff doesn't work like it should.
  39. #  .listform.list getValues -doubleClickInterval c
  40. #  set wait [expr {(2 * $c) / 1000}]
  41. #  if {$wait < 1} {
  42. #    set wait 1
  43. #  }
  44. #  exec sleep $wait
  45.  
  46.   # so busy wait instead
  47.   set n 0
  48.   while {$n < 10000} {incr n}
  49. }
  50.  
  51. proc getIO {} {
  52.   puts stdout "Press a key"
  53.   gets stdin s
  54. }
  55.  
  56. proc buttonPress {w n} {
  57.   # simulate a button press 
  58.   return [$w callActionProc ListBeginSelect() \
  59.         -type ButtonPress \
  60.         -x 0 -y [indexToY $w $n]]
  61. }
  62.  
  63. proc buttonRelease {w n} {
  64.   # simulate a button release 
  65.   return [$w callActionProc ListEndSelect() \
  66.         -type ButtonRelease \
  67.         -x 0 -y [indexToY $w $n]]
  68. }
  69.  
  70. #############
  71. # starts here
  72. #############
  73.  
  74. xtAppInitialize -class List
  75. . setValues -allowShellResize true
  76.  
  77. xmForm .listform managed
  78. xmList .listform.list managed \
  79.   -visibleItemCount 4 \
  80.   -items "a b c d" -itemCount 4
  81.  
  82. . realizeWidget
  83.  
  84. ######################
  85. # List element setting
  86. ######################
  87.  
  88. test list-1.1 {list items} {
  89.     listItems
  90. } {4 a b c d}
  91.  
  92. test list-1.2 {resetting list items} {
  93.     .listform.list setValues \
  94.     -items "A B C D E" -itemCount 5
  95.     listItems
  96. } {5 A B C D E}
  97.  
  98. test list-1.3 {clearing list items} {
  99.     .listform.list setValues \
  100.     -items "" -itemCount 0
  101.     listItems
  102. } {0 }
  103.  
  104. test list-1.4 {resetting list items} {
  105.     .listform.list setValues \
  106.     -items "a b c d" -itemCount 4
  107.     listItems
  108. } {4 a b c d}
  109.  
  110. ################################
  111. # Selecting list items by code #
  112. ################################
  113.  
  114. ########
  115. # select
  116. ########
  117. test list-2.1 {selecting list item} {
  118.     .listform.list setValues \
  119.     -selectedItems "b" -selectedItemCount 1
  120.     listSelectedItems
  121. } {1 b}
  122.  
  123. ##########
  124. # reselect
  125. ##########
  126. test list-2.2 {reselecting list items} {
  127.     .listform.list setValues \
  128.     -selectedItems "a c" -selectedItemCount 2
  129.     listSelectedItems
  130. } {2 a c}
  131.  
  132. #######
  133. # clear
  134. ######
  135. test list-2.3 {clearing list selection} {
  136.     .listform.list setValues \
  137.     -selectedItems "" -selectedItemCount 0
  138.     listSelectedItems
  139. } {0 }
  140.  
  141. ##########
  142. # reselect
  143. ##########
  144. test list-2.4 {reselecting list items} {
  145.     .listform.list setValues \
  146.     -selectedItems "a b c d" -selectedItemCount 4
  147.     listSelectedItems
  148. } {4 a b c d}
  149.  
  150. ###################################
  151. # Modifying list items by methods #
  152. ###################################
  153.  
  154. ########
  155. # Adding
  156. ########
  157.  
  158. test list-3.1 {adding list item at front} {
  159.     .listform.list addItem aa 1
  160.     listItems
  161. } {5 aa a b c d}
  162.  
  163. test list-3.2 {adding list item at end} {
  164.     .listform.list addItem e 0
  165.     listItems
  166. } {6 aa a b c d e}
  167.  
  168. test list-3.3 {adding list item in middle} {
  169.     .listform.list addItem bb 4
  170.     listItems
  171. } {7 aa a b bb c d e}
  172.  
  173. ##########
  174. # Deleting
  175. ##########
  176.  
  177. test list-3.4 {deleting list item at front} {
  178.     .listform.list deleteItem aa
  179.     listItems
  180. } {6 a b bb c d e}
  181.  
  182. test list-3.5 {deleting list item at middle} {
  183.     .listform.list deleteItem c
  184.     listItems
  185. } {5 a b bb d e}
  186.  
  187. if {$XmVersion >= 1002} {
  188. test list-3.6 {deleting all list items} {
  189.     .listform.list deleteAllItems
  190.     listItems
  191. } {0 }
  192. }
  193.  
  194. test list-3.7 {adding item to empty list} {
  195.     .listform.list setValues -items "" -itemCount 0
  196.     .listform.list addItem a 1
  197.     listItems
  198. } {1 a}
  199.  
  200. test list-3.8 {deleting list item by position} {
  201.     .listform.list deletePosition 1
  202.     listItems
  203. } {0 }
  204.  
  205.  
  206. ####################
  207. # callback testing #
  208. ####################
  209.  
  210. .listform.list setValues \
  211.     -items "a b c d" -itemCount 4
  212. .listform.list singleSelectionCallback listSelectedItems
  213.  
  214. test list-4.1 {single selection callback} {
  215.     .listform.list setValues -selectionPolicy single_select \
  216.     -selectedItems "" -selectedItemCount 0
  217.     buttonPress .listform.list 1
  218.     buttonRelease .listform.list 1
  219. } {1 a}
  220.  
  221. test list-4.2 {single selection callback} {
  222.     pause
  223.     .listform.list setValues -selectionPolicy single_select \
  224.     -selectedItems "" -selectedItemCount 0
  225.     buttonPress .listform.list 2
  226.     buttonRelease .listform.list 2
  227. listSelectedItems
  228. } {1 b}
  229.  
  230. test list-4.3 {single selection callback} {
  231.     pause
  232.     .listform.list setValues -selectionPolicy single_select \
  233.     -selectedItems "" -selectedItemCount 0
  234.     buttonPress .listform.list 4
  235.     buttonRelease .listform.list 4
  236. } {1 d}
  237.  
  238. .listform.list destroyWidget
  239.  
  240. ############################
  241. # % substitutions for list #
  242. ############################
  243.  
  244. proc singleCB {item item_position} {
  245.   return "$item $item_position"
  246. }
  247.  
  248. proc multipleCB {selected_items} {
  249.   return $selected_items
  250. }
  251.  
  252. xmList .listform.list2 managed \
  253.   -items "a b c d" -itemCount 4 \
  254.   -visibleItemCount 4
  255.  
  256. .listform.list2 singleSelectionCallback {singleCB %item %item_position}
  257. .listform.list2 multipleSelectionCallback {multipleCB {%selected_items}}
  258.  
  259. ###############
  260. # Single select
  261. ###############
  262. test list-5.1 {% substitutions for single select} {
  263.     .listform.list2 setValues \
  264.     -selectionPolicy single_select \
  265.     -selectedItems "" \
  266.     -selectedItemCount 0
  267.     buttonPress .listform.list2 2
  268.     buttonRelease .listform.list2 2
  269. } {b 2}
  270.  
  271. #################
  272. # Multiple select
  273. #################
  274. test list-5.2 {% substitutions for multiple select} {
  275.     pause
  276.     .listform.list2 setValues \
  277.     -selectionPolicy multiple_select \
  278.     -selectedItems "" \
  279.     -selectedItemCount 0
  280.     buttonPress .listform.list2 2
  281.     buttonRelease .listform.list2 2
  282.     pause
  283.     buttonPress .listform.list2 3
  284.     buttonRelease .listform.list2 3
  285. } {b c}
  286.  
  287. .listform.list2 destroyWidget
  288.  
  289. ###############################
  290. # Selection of items by methods
  291. ###############################
  292.  
  293. xmList .listform.list3 managed
  294.  
  295. .listform.list3 setValues \
  296.     -items "a b c d" \
  297.     -itemCount 4 \
  298.     -selectedItems "" \
  299.     -selectedItemCount 0 \
  300.     -selectionPolicy single_select
  301.  
  302. test list-6.1 {selectItem - single select} {
  303.     .listform.list3 selectItem a false
  304.     .listform.list3 selectItem c true
  305.     
  306.     listSelectedItems
  307. } {1 c}
  308.  
  309. test list-6.2 {selectItem - multiple select} {
  310.     .listform.list3 setValues \
  311.         -items "a b c d" \
  312.         -itemCount 4 \
  313.         -selectedItems "" \
  314.         -selectedItemCount 0 \
  315.     -selectionPolicy multiple_select
  316.  
  317.     .listform.list3 selectItem c false
  318.     .listform.list3 selectItem a false
  319.     
  320.     listSelectedItems
  321. } {2 a c}
  322.  
  323. #################################
  324. # Deselection of items by methods
  325. #################################
  326.  
  327. test list-7.1 {deselectItem - multiple select} {
  328.     .listform.list3 setValues \
  329.         -items "a b c d" \
  330.         -itemCount 4 \
  331.         -selectedItems "a b d" \
  332.         -selectedItemCount 3 \
  333.     -selectionPolicy multiple_select
  334.  
  335.     .listform.list3 deselectItem b
  336.     
  337.     listSelectedItems
  338. } {2 a d}
  339.  
  340. test list-7.2 {deselectAllItems} {
  341.     .listform.list3 deselectAllItems
  342.  
  343.     listSelectedItems
  344. } {0 }
  345.  
  346. ################################
  347. # Selection of items by position
  348. ################################
  349.  
  350. .listform.list3 deselectAllItems
  351. .listform.list3 setValues \
  352.     -selectionPolicy multiple_select \
  353.     -items "a b c d" \
  354.     -itemCount 4 \
  355.     -selectedItems "" \
  356.     -selectedItemCount 0
  357.  
  358. test list-8.1 {selectPosition} {
  359.     .listform.list3 selectPosition 1 false
  360.     .listform.list3 selectPosition 3 false
  361.  
  362.     listSelectedItems
  363. } {2 a c}
  364.  
  365. test list-8.2 {deselectPosition} {
  366.     .listform.list3 deselectPosition 1
  367.  
  368.     listSelectedItems
  369. } {1 c}
  370.  
  371. if {$XmVersion >= 1002} {
  372. test list-8.3 {positionSelected - true} {
  373.     .listform.list3 positionSelected 3
  374. } {true}
  375.  
  376.  
  377. test list-8.4 {positionSelected - false} {
  378.     .listform.list3 positionSelected 2
  379. } {false}
  380. }
  381.  
  382. ################
  383. # Item existence
  384. ################
  385.  
  386. test list-9.1 {itemExists - true} {
  387.     .listform.list3 itemExists c
  388. } {true}
  389.  
  390. test list-9.2 {itemExists - false} {
  391.     .listform.list3 itemExists ccccc
  392. } {false}
  393.  
  394.  
  395. #############
  396. # Finish up #
  397. #############
  398. if { ! $INTERACTIVE} {
  399.   .listform.list destroyWidget
  400.   .listform destroyWidget
  401. } else {
  402.   . mainLoop
  403. }
  404.